<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MController extends Controller {
	
	var $messages = array();
	
	function __construct () {
		parent::Controller();

		$this->load->library('session');
		if ($sessMsgs = $this->session->userdata('messages')) { $this->messages = $sessMsgs; }
		$this->session->unset_userdata('messages');
		$this->load->model('Accessmodel');
		$this->load->helper('url');
		$this->lang->load('layout', 'Russian');
		$this->lang->load('common', 'Russian');
		$this->Accessmodel->urls = $this->uri->segment_array();
		$this->Accessmodel->user_id = (int)$this->session->userdata('user_id');
		if (!$this->Accessmodel->check()) {
			$this->setMessage($this->lang->line('access_denied'), 'error');
			redirect('user/login');
		}
		return ;
	}
	
	function setMessage ($message, $type = 'message') {
		$messages = $this->session->userdata('messages');
		if (empty($messages)) { $messages = array(); }
		if (in_array($type, array('form'))) { $messages[$type] = $message; }
		else { $messages[$type][] = $message; }
		$this->session->set_userdata('messages', $messages);
		$this->messages = array_merge($this->messages, $messages);
		return true;
	}
	
	function getMessages () {
		$this->session->unset_userdata('messages');
		$res = $this->messages;
		$this->messages = array();
		return $res;
	}
	
	function display ($data, $tpl = 'layout') {
		
		// default labels:
		//	 topmenu
		//	 legend
		//	 general_menu
		//	 content
		//	 footer_text
		//	 messages
		
		if (!is_array($data)) {
			$data = array('content' => $data);
		}
		
		if (!array_key_exists('main_menu', 		$data)) { $data['main_menu'] 		= $this->mmenu->menu('topmenu'); }
		if (!array_key_exists('general_menu', 	$data)) { $data['general_menu'] = $this->mmenu->menu('general_menu'); }
		if (!array_key_exists('legend',			$data)) { $data['legend'] 		= $this->lang->line('page_header_text'); }
		if (!array_key_exists('footer_text', 	$data)) { $data['footer_text'] 	= $this->lang->line('page_footer_text'); }
		if (!array_key_exists('page_title', 	$data)) { $data['page_title'] 	= $this->lang->line('page_title'); }

		if (!array_key_exists('content', 		$data)) { $data['content'] 		= ''; }
		if (!array_key_exists('left_column', 	$data)) { $data['left_column'] 	= ''; }
		if (!array_key_exists('right_column', 	$data)) { $data['right_column'] = ''; }
		if (!array_key_exists('messages', 		$data)) { $data['messages'] = $this->load->view('messages', array('messages' => $this->getMessages()), true); }
		if (!array_key_exists('scripts', 		$data)) { $data['scripts'] = $this->prepareScripts(); }
		
		$this->load->view($tpl, $data);
	}
	
	function prepareScripts ($js = null, $include = true) {
		static $scripts = array();
		if (empty($js)) { return join($scripts); } 
		else {
			if ($include) { $scripts[] = '<script src="' . $js . '" type="text/javascript"></script> '; } 
			else { $scripts[] = $js; }
		}
	}
}

?>
